//
//  SINAMainMsgViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/28.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "SINAMainMsgViewController.h"
#import "SINAMsgItemTableViewCell.h"
#import "SINACourseSigneModel.h"
#import "SINAMsgItemModel.h"
#import "SINAMsgDetaileViewController.h"
#import "SINAMsgItemModel.h"

@interface SINAMainMsgViewController () <UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *mainContentTableView;
@property(nonatomic,strong) NSMutableArray *dataList;

@property(nonatomic,assign) NSInteger pageIndex;

@end

@implementation SINAMainMsgViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"消息通知";
    
    self.mainContentTableView.delegate=self;
    self.mainContentTableView.dataSource=self;
    self.mainContentTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.mainContentTableView.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
    self.mainContentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    
    
    
    //下拉刷新
    self.mainContentTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadingNewData)];
    //上拉刷新
    self.mainContentTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadingMoreData)];
    
    //自动更改透明度
    self.mainContentTableView.mj_header.automaticallyChangeAlpha = YES;
    
    //进入刷新状态
    //[self.mainContentTableView.mj_header beginRefreshing];
    
    [self.mainContentTableView.mj_header setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
    [self.mainContentTableView.mj_footer setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
    // Do any additional setup after loading the view from its nib.
    
    [self loadingNetData];
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//下拉刷新
-(void) loadingNewData{
    
    
    
   
    //结束头部刷新
     [self.mainContentTableView.mj_header endRefreshing];
        [self.dataList removeAllObjects];
        [self.mainContentTableView reloadData];
        [self loadingNetData];
}

//上拉加载更多
-(void) loadingMoreData{
    
    //    __weak typeof(self) weakSelf = self;
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        //结束尾部刷新
    //        [weakSelf.mainContentTableView.mj_footer endRefreshing];
    //    });
    
//    [self.dataList removeAllObjects];
//    [self.mainContentTableView reloadData];
//    [self.mainContentTableView.mj_footer endRefreshing];
//    [self loadingNetData];
    
    __weak typeof(self) weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //结束头部刷新
        [weakSelf.mainContentTableView.mj_footer endRefreshing];
        
        [YPXToastView showToastViewWithText:@"暂无更多消息数据" andDuration:2 andParentView:weakSelf.view];
    });
}
-(NSMutableArray *)dataList{
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return  _dataList;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    SINAMsgItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SINAMsgItemTableViewCell"];
    
    if (cell == nil) {
        cell= [[[NSBundle mainBundle]loadNibNamed:@"SINAMsgItemTableViewCell" owner:nil options:nil] firstObject];
        
    }
    
    
    SINAMsgItemModel *model=self.dataList[indexPath.row];
    cell.cellMsgClickBlock = ^(NSInteger pos, SINAMsgItemModel *itemModel) {
        SINAMsgDetaileViewController *controll = [[SINAMsgDetaileViewController alloc]initWithNibName:@"SINAMsgDetaileViewController" bundle:nil];
        controll.model=itemModel;
        [self.navigationController pushViewController:controll animated:YES];
    };
    cell.pos=indexPath.row;
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell ;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 104;
}
//查看详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    //    NSInteger index = indexPath.row;
    //    SINAMsgItemModel * model = self.dataList[index];
    
    
}

/**
 *分页加载数据
 */
-(void)loadingNetData{
    
    
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setObject:[NSString stringWithFormat:@"%ld",(long)self.pageIndex] forKey:@"pageIndex"];
    [dict setObject:[NSString stringWithFormat:@"200"] forKey:@"pageSize"];
    [dict setObject:[NSString stringWithFormat:@"%@", [[SINACommonUserHelper share]getUserModel].userUid] forKey:@"receiveruuid"];
    
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@",baseUrl,stuGetMsgListUrl] withParamters:dict success:^(id responseData) {
        
        NSString *code = responseData[@"code"];
        
        if ([code isEqualToString:@"1000"]) {
            NSMutableArray *result = responseData[@"content"];
            
            for (NSInteger i=0; i<result.count; i++) {
                NSDictionary *dict = result[i];
                SINAMsgItemModel *model = [[SINAMsgItemModel alloc]initWithDict:dict];
                [self.dataList addObject:model];
            }
            [self.mainContentTableView reloadData];
        }else{
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
        }
        
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"加载数据失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
    }];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
